package com.hcms.system.controller;

import com.hcms.common.data.Tags;
import com.hcms.common.utils.Result;
import com.hcms.system.domain.AdminDo;
import com.hcms.system.domain.AdminRoleDo;
import com.hcms.system.domain.RoleDo;
import com.hcms.system.service.AdminRoleService;
import com.hcms.system.service.AdminService;
import com.hcms.system.service.RoleService;
import com.hcms.system.service.impl.AdminServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/system/admin")
@Controller
public class AdminController {
    String prefix = "system/admin";

    @Autowired
    AdminService adminService;

    @Autowired
    AdminRoleService adminRoleService;

    @Autowired
    RoleService roleService;

    @GetMapping()
    public String admin() {
        return prefix + "/admin";
    }

    @PostMapping("/list")
    @ResponseBody
    public Result list(int searchMarkStatus, String searchName, String searchKey,
                       int page, int limit) {

        Map<String, Object> queryParams = new HashMap<>();

        if (searchName != null && searchKey != null) {
            switch (searchName) {
                case "id":
                    queryParams.put("id", searchKey);
                    break;
                case "name":
                    queryParams.put("name", searchKey);
                    break;
            }
        }
        queryParams.put("markStatus", searchMarkStatus);
        queryParams.put("offset", (page - 1) * limit);
        queryParams.put("limit", limit);

        List<AdminDo> admins = adminService.list(queryParams);
        int count = adminService.count(queryParams);

        if (admins != null) {
            admins.forEach(t -> {
                AdminRoleDo adminRoleDo = adminRoleService.getByAdmin(t.getId());
                if (adminRoleDo != null) {
                    RoleDo roleDo = roleService.get(adminRoleDo.getRoleId());
                    if (roleDo != null) {
                        t.setRoleName(roleDo.getName());
                    }
                }
            });
        }

        HashMap<String, Object> result = new HashMap<>();
        result.put("count", count);
        result.put("data", admins);

        return Result.ok(result);
    }

    @GetMapping("/add/")
    public String add(Model model) {
        model.addAttribute("markStatusData", new Tags().markStatusData());

        return prefix + "/add";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result save(AdminDo admin) {
        admin.setCreateUser("admin");
        admin.setUpdateUser("admin");
        int count = adminService.save(admin);
        if (count > 0) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @GetMapping("/edit/{id}/")
    public String edit(@PathVariable("id") int id, Model model) {
        AdminDo adminDo = adminService.get(id);

        int adminRoleId = 0;
        AdminRoleDo adminRoleDo = adminRoleService.getByAdmin(id);
        if (adminRoleDo != null) {
            adminRoleId = adminRoleDo.getRoleId();
        }

        model.addAttribute("admin", adminDo);
        model.addAttribute("markStatusData", new Tags().markStatusData());
        model.addAttribute("adminRoleId", adminRoleId);

        return prefix + "/edit";
    }

    @PostMapping("/update/")
    @ResponseBody()
    public Result update(AdminDo admin, int roleId) {
        admin.setUpdateUser("admin");
        int count = adminService.update(admin);
        if (count > 0) {
            // 更新管理员角色
            adminRoleService.setRoleByAdmin(admin.getId(), roleId, "admin");

            return Result.ok();
        } else {
            return Result.error();
        }
    }
}
